#pragma once
#include <mutex>
#include "util.hpp"
namespace Cloud
{
    #define CONFIG_FILE "./cloud.conf"
    // 单例模式，懒汉模式
    class Config
    {
        Config()
        {
            ReadConfigFile();
        }
        bool ReadConfigFile()
        {
            Cloud::FileUtil fu(CONFIG_FILE);
            std::string body;
            if(fu.GetContent(&body)==false)
            {
                std::cerr<<"load config file failed!"<<std::endl;
                return false;
            }
            Json::Value root;
            if(Cloud::JsonUtil::Deserialize(body,&root)==false)
            {
                std::cerr<<"config file deserialize failed!"<<std::endl;
                return false;
            }
            _hot_time=root["hot_time"].asInt();
            _server_ip=root["server_ip"].asString();
            _servere_port=root["server_port"].asInt();
            _download_prefix=root["download_prefix"].asString();
            _packfile_suffix=root["packfile_suffix"].asString();
            _pack_dir=root["pack_dir"].asString();
            _backup_dir=root["backup_dir"].asString();
            _backup_file=root["backup_file"].asString();
            return true;
        }

    public:
        static Config* GetInstance()
        {
            if(_instance==nullptr)
            {
                _mutex.lock();
                if(_instance==nullptr)
                {
                    _instance=new Config();
                }
                _mutex.unlock();
            }
            return _instance;
        }
        int GetHotTime()
        {
            return _hot_time;
        }
        int GetServerPort()
        {
            return _servere_port;
        }
        std::string GetServerIp()
        {
            return _server_ip;
        }
        std::string GetDownloadPrefix()
        {
            return _download_prefix;
        }
        std::string GetPackfileSuffix()
        {
            return _packfile_suffix;
        }
        std::string GetPackDir()
        {
            return _pack_dir;
        }
        std::string GetBackupDir()
        {
            return _backup_dir;
        }
        std::string GetBackupFile()
        {
            return _backup_file;
        }

    private:
        static Config *_instance;
        static std::mutex _mutex;

    private:
        int _hot_time;
        int _servere_port;
        std::string _server_ip;
        std::string _download_prefix;
        std::string _packfile_suffix;
        std::string _pack_dir;
        std::string _backup_dir;
        std::string _backup_file;
    };
    Config* Config::_instance=nullptr;
    std::mutex Config::_mutex; 
}
